<template>
  <div>
    <!-- 轮播图区域 -->
    <div v-if="banners.length > 0" class="slider-container">
      <div class="slider-wrapper" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
        <div class="slider-item" v-for="(item, index) in banners" :key="index">
          <a :href="item.linkUrl" target="_blank">
            <img :src="item.imageUrl" class="slider-img" :alt="item.title" @error="handleImageError">
            <div class="slider-caption">
              <!--              <h3>{{ item.title }}</h3>-->
              <p v-if="item.description">{{ item.description }}</p>
            </div>
          </a>
        </div>
      </div>
      <button class="slider-btn prev" @click="prevSlide" aria-label="Previous slide">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
          <path d="M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z"/>
        </svg>
      </button>
      <button class="slider-btn next" @click="nextSlide" aria-label="Next slide">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
          <path d="M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z"/>
        </svg>
      </button>
      <div class="slider-dots">
        <button v-for="(dot, index) in banners" :key="'dot' + index" :class="{ active: currentIndex === index }"
                @click="goToSlide(index)" :aria-label="`Go to slide ${index + 1}`"></button>
      </div>
    </div>
    <!-- 加载状态 -->
    <div v-else class="slider-loading">
      <div class="loading-spinner"></div>
      <p>加载轮播图中...</p>
    </div>
    <!-- 幻灯片 结束 -->

    <div id="aCoursesList">
      <!-- 网校课程 开始 -->
      <div>
        <section class="container">
          <header class="comm-title">
            <h2 class="tac">
              <span class="c-333"><em>最新课程</em></span>
            </h2>
          </header>
          <div>
            <article class="comm-course-list">
              <ul class="of" id="bna">

                <li v-for="course in courses" :key="course.id">
                  <div class="cc-l-wrap">
                    <section class="course-img">
                      <img
                        :src="course.cover"
                        class="img-responsive"
                        :alt="course.title"
                      >
                      <div class="cc-mask">
                        <a href="#" title="开始学习" class="comm-btn c-btn-1">开始学习</a>
                      </div>
                    </section>
                    <h3 class="hLh30 txtOf mt10">
                      <a href="#" :title="course.title" class="course-title fsize18 c-333">{{ course.title }}</a>
                    </h3>
                    <section class="mt10 hLh20 of">
      <span class="fr jgTag bg-green" v-if="Number(course.price) === 0">
        <i class="c-fff fsize12 f-fA">免费</i>
      </span>
                      <span class="fr jgTag bg-green" v-else>
        <i class="c-fff fsize12 f-fA">￥{{ course.price }}</i>
      </span>
                      <span class="fl jgAttr c-ccc f-fA">
        <i class="c-999 f-fA">{{ course.buyCount }} 人学习</i>
        |
        <i class="c-999 f-fA">{{ course.viewCount }} 人浏览</i>
      </span>
                    </section>
                  </div>
                </li>
              </ul>
              <div class="clear"></div>
            </article>
            <section class="tac pt20">
              <a href="#" title="全部课程" class="comm-btn c-btn-2">全部课程</a>
            </section>
          </div>
        </section>
      </div>

      <!-- /网校课程 结束 -->

      <!-- 网校名师 开始 -->
      <div>
        <section class="container">
          <header class="comm-title">
            <h2 class="tac">
              <span class="c-333">名师大咖</span>
            </h2>
          </header>
          <div>
            <article class="i-teacher-list">
              <ul class="of">
                <li v-for="teacher in teachers" :key="teacher.id">
                  <section class="i-teach-wrap">
                    <div class="i-teach-pic">
                      <a href="/teacher/1" :title="teacher.name">
                        <img :alt="teacher.name" :src="teacher.avatar">
                      </a>
                    </div>
                    <div class="mt10 hLh30 txtOf tac">
                      <a href="/teacher/1" :title="teacher.name" class="fsize18 c-666">{{ teacher.name }}</a>
                    </div>
                    <div class="hLh30 txtOf tac">
                      <span class="fsize14 c-999">{{ teacher.career }}</span>
                    </div>
                    <div class="mt15 i-q-txt">
                      <p
                        class="c-999 f-fA"
                      >{{ teacher.intro }}</p>
                    </div>
                  </section>
                </li>
              </ul>
              <div class="clear"></div>
            </article>
            <section class="tac pt20">
              <a href="#" title="全部讲师" class="comm-btn c-btn-2">全部讲师</a>
            </section>
          </div>
        </section>
      </div>

      <!-- /网校名师 结束 -->
    </div>
  </div>
</template>

<script>
import bannerApi from '@/api/banner'
import indexApi from '@/api/index'

export default {
  data() {
    return {
      banners: [],
      courses: [],
      teachers: [],
      currentIndex: 0,
      intervalId: null,
    }
  },
  created() {
    this.loadBannerData()
    this.getCourseAndTeacher();
  },
  mounted() {
    this.startAutoSlide()
  },
  beforeDestroy() {
    clearInterval(this.intervalId)
  },
  methods: {
    getCourseAndTeacher() {
      indexApi.getCourseAndTeacher().then(response => {
        console.log("getCourseAndTeacher:", response.data.data)
        this.teachers = response.data.data.teacherList.records
        this.courses = response.data.data.courseList.records
      })
    },

    loadBannerData() {
      bannerApi.findAllBanner().then(response => {
        if (response.data && response.data.data) {
          this.banners = response.data.data
          // 如果只有一张图，不需要自动播放
          if (this.banners.length <= 1) {
            clearInterval(this.intervalId)
          }
        }
      }).catch(error => {
        console.error('加载轮播图失败:', error)
      })
    },
    startAutoSlide() {
      // 确保有足够数量的banner才启动自动播放
      if (this.banners.length > 1) {
        this.intervalId = setInterval(() => {
          this.nextSlide()
        }, 3000)
      }
    },
    nextSlide() {
      if (this.banners.length === 0) return
      this.currentIndex = (this.currentIndex + 1) % this.banners.length
    },
    prevSlide() {
      if (this.banners.length === 0) return
      this.currentIndex = (this.currentIndex - 1 + this.banners.length) % this.banners.length
    },
    goToSlide(index) {
      this.currentIndex = index
    },
    handleImageError(e) {
      e.target.src = require('~/assets/img/mysvg/logo.png')
    }
  }
}
</script>

<style scoped>
/* 改进后的轮播图样式 */
.slider-container {
  position: relative;
  width: 80%;
  max-width: 1200px;
  height: 500px;
  overflow: hidden;
  margin: 0 auto 40px;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}

.slider-wrapper {
  display: flex;
  height: 100%;
  transition: transform 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.slider-item {
  position: relative;
  min-width: 100%;
  height: 100%;
}

.slider-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
}

.slider-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 30px;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
  color: white;
  transform: translateY(0);
  transition: transform 0.3s ease;
}

.slider-caption h3 {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.slider-caption p {
  font-size: 16px;
  opacity: 0.9;
  margin-bottom: 0;
  max-width: 70%;
}

.slider-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  border: none;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  backdrop-filter: blur(5px);
}

.slider-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-50%) scale(1.1);
}

.slider-btn svg {
  width: 24px;
  height: 24px;
}

.slider-btn.prev {
  left: 30px;
}

.slider-btn.next {
  right: 30px;
}

.slider-dots {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 10px;
  z-index: 10;
}

.slider-dots button {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.5);
  border: none;
  padding: 0;
  cursor: pointer;
  transition: all 0.3s ease;
}

.slider-dots button.active {
  background-color: #fff;
  transform: scale(1.2);
}

.slider-dots button:hover:not(.active) {
  background-color: rgba(255, 255, 255, 0.7);
}

/* 加载状态样式改进 */
.slider-loading {
  height: 500px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #f8f9fa;
  border-radius: 12px;
  margin: 0 auto 40px;
  width: 80%;
  max-width: 1200px;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 4px solid rgba(59, 128, 112, 0.2);
  border-radius: 50%;
  border-top-color: #3B8070;
  animation: spin 1s linear infinite;
  margin-bottom: 15px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 响应式调整 */
@media (max-width: 992px) {
  .slider-container, .slider-loading {
    width: 90%;
    height: 400px;
  }

  .slider-caption {
    padding: 20px;
  }

  .slider-caption h3 {
    font-size: 24px;
  }
}

@media (max-width: 768px) {
  .slider-container, .slider-loading {
    height: 350px;
    width: 95%;
  }

  .slider-btn {
    width: 40px;
    height: 40px;
  }

  .slider-btn svg {
    width: 20px;
    height: 20px;
  }

  .slider-caption h3 {
    font-size: 20px;
  }

  .slider-caption p {
    font-size: 14px;
    max-width: 90%;
  }
}

@media (max-width: 576px) {
  .slider-container, .slider-loading {
    height: 300px;
  }

  .slider-caption {
    padding: 15px;
  }

  .slider-caption h3 {
    font-size: 18px;
  }

  .slider-dots button {
    width: 10px;
    height: 10px;
  }
}
</style>
